package com.youmingxing.adpter.RecyclerViewAdapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youmingxing.R;
import com.youmingxing.bean.selfinfo.AuditionRank;

import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * @author evilyin(ChenZhixi)
 * @since 16/1/21
 */
public class RankRecyclerViewAdapter extends RecyclerView.Adapter<RankRecyclerViewAdapter.ViewHolder>{

    private List<AuditionRank> dataList;
    private Context context;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        TextView nameTv;
        TextView rankingnumTv;
        TextView ticketnumTv;

        ImageView icon;
    }

    public RankRecyclerViewAdapter(Context context, List<AuditionRank> list) {
        this.context = context;
        dataList = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_audition_top4, null);
        ViewHolder holder = new ViewHolder(view);
        holder.nameTv = (TextView) view.findViewById(R.id.actor_name);
        holder.rankingnumTv = (TextView) view.findViewById(R.id.top_rank);
        holder.ticketnumTv = (TextView) view.findViewById(R.id.vote_number);
        holder.icon = (ImageView) view.findViewById(R.id.actor_image);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        AuditionRank rank = dataList.get(position);
        holder.nameTv.setText(rank.nickname);
        holder.ticketnumTv.setText(rank.votes + "票");
        Glide.with(context).load(rank.headimg)
                .placeholder(R.drawable.default_avatar).error(R.drawable.default_avatar)
                .bitmapTransform(new CropCircleTransformation(context))
                .crossFade().into(holder.icon);
        if (rank.status == 2) {
            holder.rankingnumTv.setText("已入围");
            holder.rankingnumTv.setTextColor(Color.parseColor("#EA4141"));
            holder.rankingnumTv.setBackgroundResource(R.drawable.btn_red_edge_radius20);
        } else {
            int rankInt = position + 1;
            holder.rankingnumTv.setText("TOP" + rankInt);
            holder.rankingnumTv.setBackgroundResource(R.drawable.roundconner_yellow);
            holder.rankingnumTv.setTextColor(Color.parseColor("#fdfdfd"));
        }

        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

}
